package com.jprocms.module.cms.controller.admin.type;

import com.jpro.framework.apilog.core.annotation.ApiAccessLog;
import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageParam;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jprocms.module.cms.controller.admin.type.vo.CmsTypePageReqVO;
import com.jprocms.module.cms.controller.admin.type.vo.CmsTypeRespVO;
import com.jprocms.module.cms.controller.admin.type.vo.CmsTypeSaveReqVO;
import com.jprocms.module.cms.dal.dataobject.type.CmsTypeDO;
import com.jprocms.module.cms.service.type.CmsTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.jpro.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.jpro.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 内容类型")
@RestController
@RequestMapping("/cms/type")
@Validated
public class CmsTypeController {

    @Resource
    private CmsTypeService typeService;

    @PostMapping("/create")
    @Operation(summary = "创建内容类型")
    @PreAuthorize("@ss.hasPermission('cms:type:create')")
    public CommonResult<Long> createType(@Valid @RequestBody CmsTypeSaveReqVO createReqVO) {
        return success(typeService.createType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内容类型")
    @PreAuthorize("@ss.hasPermission('cms:type:update')")
    public CommonResult<Boolean> updateType(@Valid @RequestBody CmsTypeSaveReqVO updateReqVO) {
        typeService.updateType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内容类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:type:delete')")
    public CommonResult<Boolean> deleteType(@RequestParam("id") Long id) {
        typeService.deleteType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内容类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:type:query')")
    public CommonResult<CmsTypeRespVO> getType(@RequestParam("id") Long id) {
        CmsTypeDO type = typeService.getType(id);
        return success(BeanUtils.toBean(type, CmsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内容类型分页")
    @PreAuthorize("@ss.hasPermission('cms:type:query')")
    public CommonResult<PageResult<CmsTypeRespVO>> getTypePage(@Valid CmsTypePageReqVO pageReqVO) {
        PageResult<CmsTypeDO> pageResult = typeService.getTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CmsTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得内容类型列表")
    public CommonResult<List<CmsTypeRespVO>> getTypeList(@Valid CmsTypePageReqVO pageReqVO) {
        List<CmsTypeDO> result = typeService.getTypeList(pageReqVO);
        return success(BeanUtils.toBean(result, CmsTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内容类型 Excel")
    @PreAuthorize("@ss.hasPermission('cms:type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTypeExcel(@Valid CmsTypePageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmsTypeDO> list = typeService.getTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内容类型.xls", "数据", CmsTypeRespVO.class,
                BeanUtils.toBean(list, CmsTypeRespVO.class));
    }

}